<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>管理员登录</title>
    <meta name="renderer" content="webkit|ie-comp|ie-stand">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <meta http-equiv="Cache-Control" content="no-siteapp" />
    <link rel="shortcut icon" href="./favicon.ico" type="image/x-icon" />
    <link rel="stylesheet" href="./static/css/font.css">
    <link rel="stylesheet" href="./static/css/weadmin.css">
    <script src="./lib/layui/layui.js" charset="utf-8"></script>
    <script src="../../assets/js/vendor/jquery-3.3.1.min.js"></script>
    <script src="../../assets/js/plugins/utils.js"></script>

</head>
<body class="login-bg">

<div class="login">
    <div class="message">管理登录</div>
    <div id="darkbannerwrap"></div>

    <div class="layui-form" >
        <input name="username" placeholder="用户名"  type="text" id="username" class="layui-input" >
        <hr class="hr15">
        <input name="password" placeholder="密码"  type="password" class="layui-input" id="password">
        <hr class="hr15">
        <input class="loginin" value="登录"  style="width:100%;" lay-submit lay-filter="login" type="submit" onclick="login()">
        <hr class="hr20" >
    </div>
</div>

<script type="text/javascript">
    layui.extend({
        admin: '{/}./static/js/admin'
    });
    function login() {
        let username = $('#username').val();
        let password = $('#password').val();
        $.ajax({
            url: "/user/login",
            type: "POST",
            data: JSON.stringify({
                "email": username,
                "password": password
            }),
            contentType: "application/json;charset=utf-8",
            success: function (response) {
                if (response.code == 200) {
                    let token = response.data.token;
                    let nickname = response.data.nickname;
                    $.ajax({
                        url: "/admin/auth",
                        type: "get",
                        headers: {
                            "Authorization": "Bearer " + token
                        },
                        success: function (response) {
                            if (response.code == 200) {
                                handleLocalStorage("set", "token", token);
                                handleLocalStorage("set", "nickname", nickname);
                                alert("登录成功");
                                window.location.href = "/view/admin/index.html";
                            }
                            if (response.code == 18) {
                                alert("权限不足");
                                window.location.href = "/";
                            }
                        },
                        error: function (error) {
                            alert("服务器错误");
                        }
                    })

                } else {
                    alert(response.message);
                }
            },
            error: function (error) {
                alert("服务器错误");
            }
        })
    }
</script>
<!-- 底部结束 -->
</body>
</html>